﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Controller;

namespace IAI
{
    /// <summary>
    /// Interaction logic for Settings.xaml
    /// </summary>
    public partial class Settings : Window
    {
        private string _txt;
        private MyController _myController;
        private Settings1 _settings;
        private readonly List<string> _colors = new List<string>() //http://www.tinygorilla.com/Easter_eggs/PallateHex.html
            {
                "#F7977A", "#F9AD81", "#FDC68A", "#FFF79A", "#C4DF9B", "#A2D39C", "#82CA9D", "#7BCDC8", "#6ECFF6", "#7EA7D8", "#8493CA", "#8882BE", "#A187BE", "#BC8DBF", "#F49AC2", "#F6989D"
            };
        //Settings
        private readonly BrushConverter _brushConverter = new BrushConverter();
        public Settings(MyController myController)
        {
            InitializeComponent();
            _myController = myController;
            _settings = new Settings1();
            txtUpdateInterval.Text = _settings.Interval.ToString();
            SettingsBackgroundColor();
            txtDataSource.Text = _settings.DataSource;
            txtUsername.Text = _settings.Username;
            //txtPassword.Text = "".PadLeft(_settings.Password.Length, '*'); //show password as *
            txtPassword.Text = _settings.Password;
            passPassword.Password = _settings.Password;
        }

        public void SettingsBackgroundColor()
        {
            List<ComboBox> cbBackgroundColors = new List<ComboBox>() { 
                cbPLHasFailFeatureWithFailColor,
                cbPLHasFailFeatureWithoutFailColor 
                };

            foreach (ComboBox cb in cbBackgroundColors)
            {
                int count = 0, index = 0;
                foreach (string c in _colors)
                {
                    cb.Items.Add(new ComboBoxItem { Content = c, Background = String2Brush(c) });
                    {
                        if (c == (string)_settings[cb.Name])
                            index = count;
                        count++;
                    }
                }
                //if list not empty
                if (cb.Items.Count > 0)
                    cb.SelectedIndex = index;
                ComboBoxItem cbi = (ComboBoxItem)cb.SelectedItem;
                cb.Background = String2Brush(cbi.Content.ToString());
            }
        }

        public Brush String2Brush(string color) { return (Brush)_brushConverter.ConvertFrom(color); }

        #region Events
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            int interval = int.Parse(txtUpdateInterval.Text);
            _settings.Interval = interval;
            _settings.Save();
        }

        private void cbSettingsBackgroundColor_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = (ComboBox)sender;
            ComboBoxItem cbi = (ComboBoxItem)cb.SelectedItem;
            string color = (string)cbi.Content;
            cb.Background = String2Brush(color);
            //DataGridFeature(); //from uclive window
            _settings[cb.Name] = color;
            _settings.Save();
            //Tien, 30. maj
            //http://stackoverflow.com/questions/14810564/how-to-change-the-wpf-windows-lable-text-from-another-wpf-window
            Login l = Owner as Login;
            if (l != null)
            {
                l.gridContent.Children.Clear();
                l.gridContent.Children.Add(new UCLive(_myController));
            }
        }
        private void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            TextBox tempTxt = (TextBox)e.Source;
            _txt = tempTxt.Text;
            if (tempTxt.Text == "Søg")
                tempTxt.Clear();
        }

        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox tempTxt = (TextBox)e.Source;
            if (tempTxt.Text == string.Empty || tempTxt.Text.Length == 0)
                tempTxt.Text = _txt;
        }

        private void txtPassword_TextChanged(object sender, TextChangedEventArgs e)
        {/*
            string temp = "";
            txtPassword.Clear();
            if (chbPassword.IsChecked == false)
            {
                temp += txtPassword.Text.Last();
            }*/
        }

        private void chbPassword_Checked(object sender, RoutedEventArgs e)
        {
            txtPassword.Visibility = Visibility.Visible;
            passPassword.Visibility = Visibility.Hidden;
            /*
            CheckBox cb = (CheckBox)sender;
            txtPassword.Text = _settings.Password;*/
        }

        private void chbPassword_Unchecked(object sender, RoutedEventArgs e)
        {
            txtPassword.Visibility = Visibility.Hidden;
            passPassword.Visibility = Visibility.Visible;
            /*
            CheckBox cb = (CheckBox)sender;
            txtPassword.Text = "".PadLeft(_settings.Password.Length, '*');*/
        }
        #endregion
    }
}
